package com.scm.biz.action;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.scm.core.initbinder.DoubleEditor;
import com.scm.core.initbinder.FloatEditor;
import com.scm.core.initbinder.IntegerEditor;
import com.scm.core.initbinder.LongEditor;

/**
 * @Description: 基类ACTION
 * @date 2016年1月11日
 * @author 1936
 */
public class BaseAction implements Serializable {

	private static final long serialVersionUID = -5192292757373985406L;

	/**
	 * 描述: 获取Session值
	 * 
	 * @auther 胡义振
	 */
	public static Object doGetSession(String key) {
		return (Object) getSession().getAttribute(key);
	}

	/**
	 * 描述: 设置Session值
	 * 
	 * @auther 胡义振
	 */
	public static void doSetSession(String key, Object object) {
		getSession().setAttribute(key, object);
	}

	public static void doRemoveSession(String key) {
		getSession().removeAttribute(key);
	}

	// 获取 session
	private static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = doGetRequst().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	// 获取 request
	public static HttpServletRequest doGetRequst() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		//json格式提交数据类型转换
		binder.registerCustomEditor(int.class, new IntegerEditor());
		binder.registerCustomEditor(Integer.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(Long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Double.class, new DoubleEditor());
		binder.registerCustomEditor(float.class, new FloatEditor());
		binder.registerCustomEditor(Float.class, new FloatEditor());
	}
}
